package com.huawei.appgallery.downloadengine.impl;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import o.bnc;
import o.bnl;
import o.bnr;
import o.bny;
import o.ll;

public class DownloadService extends Service {
    private static List<WeakReference<Future<?>>> ˊ = new ArrayList();
    private static d ˋ = new d();
    private final IBinder ॱ = new c(this);

    public class c extends Binder {
        final /* synthetic */ DownloadService ˋ;

        public c(DownloadService downloadService) {
            this.ˋ = downloadService;
        }

        public DownloadService ॱ() {
            return this.ˋ;
        }
    }

    static class d extends SafeBroadcastReceiver {
        d() {
        }

        public void onReceiveMsg(Context context, Intent intent) {
            if (intent != null) {
                Object action = new SafeIntent(intent).getAction();
                if (!TextUtils.isEmpty(action) && action.equals("ACTION_ONE_TASK_FINISHED")) {
                    bnc.ˎ.ˋ("HiAppDownload", "DownloadTaskChecker: one task finished");
                    if (DownloadService.isAllDone()) {
                        bnc.ˎ.ˋ("HiAppDownload", "DownloadService task allDone, stop service");
                        try {
                            context.stopService(new Intent(context, DownloadService.class));
                        } catch (Exception e) {
                            bnc.ˎ.ˋ("HiAppDownload", "failed to stop service, catch a Exception");
                        }
                        bnr.ˎ();
                    }
                }
            }
        }
    }

    public void onCreate() {
        super.onCreate();
        ˊ.clear();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("ACTION_ONE_TASK_FINISHED");
        ll.ˏ(this).ˏ(ˋ, intentFilter);
        ˋ();
        bnl.ˏ().ˋॱ();
        bnc.ˎ.ˋ("HiAppDownload", "DownloadService onCreate");
    }

    private void ˋ() {
        if (!bnl.ˏ().ʼ()) {
            bnl.ˏ().ˊ(true);
            SessionDownloadTask sessionDownloadTask = new SessionDownloadTask();
            sessionDownloadTask.ॱॱ(11);
            bny.ˏ(sessionDownloadTask);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        bnl.ˏ().ˎ(false);
        ll.ˏ(this).ˋ(ˋ);
        bnc.ˎ.ˋ("HiAppDownload", "DownloadService onDestroy");
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        if (intent != null) {
            doDownload(intent.getLongExtra("sessionId", -1));
        }
        return 2;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void doDownload(long r6) {
        /*
        r5 = this;
        r0 = o.bnl.ˏ();
        r1 = r0.ˊ(r6);
        if (r1 == 0) goto L_0x0018;
    L_0x000a:
        monitor-enter(r1);
        r0 = r1.ˊᐝ();	 Catch:{ all -> 0x0079 }
        if (r0 != 0) goto L_0x0017;
    L_0x0011:
        r0 = r1.ʻ();	 Catch:{ all -> 0x0079 }
        if (r0 == 0) goto L_0x0019;
    L_0x0017:
        monitor-exit(r1);	 Catch:{ all -> 0x0079 }
    L_0x0018:
        return;
    L_0x0019:
        r0 = 0;
        r1.ॱॱ(r0);	 Catch:{ all -> 0x0079 }
        o.bny.ˏ(r1);	 Catch:{ all -> 0x0079 }
        r2 = new o.bnv;	 Catch:{ all -> 0x0079 }
        r2.<init>(r1);	 Catch:{ all -> 0x0079 }
        r0 = r1.ʼ();	 Catch:{ all -> 0x0079 }
        if (r0 != 0) goto L_0x007c;
    L_0x002b:
        r0 = o.bnl.ˏ();	 Catch:{ all -> 0x0079 }
        r0 = r0.ˏॱ();	 Catch:{ all -> 0x0079 }
        r0 = r0.submit(r2);	 Catch:{ all -> 0x0079 }
    L_0x0037:
        r3 = ˊ;	 Catch:{ all -> 0x0079 }
        r4 = new java.lang.ref.WeakReference;	 Catch:{ all -> 0x0079 }
        r4.<init>(r0);	 Catch:{ all -> 0x0079 }
        r3.add(r4);	 Catch:{ all -> 0x0079 }
        r1.ˎ(r0);	 Catch:{ all -> 0x0079 }
        r0 = 1;
        r1.ॱ(r0);	 Catch:{ all -> 0x0079 }
        r1.ˏ(r2);	 Catch:{ all -> 0x0079 }
        r0 = o.bnc.ˎ;	 Catch:{ all -> 0x0079 }
        r2 = "HiAppDownload";
        r3 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0079 }
        r3.<init>();	 Catch:{ all -> 0x0079 }
        r4 = "DownloadService submit task:";
        r3 = r3.append(r4);	 Catch:{ all -> 0x0079 }
        r4 = r1.ˊˊ();	 Catch:{ all -> 0x0079 }
        r3 = r3.append(r4);	 Catch:{ all -> 0x0079 }
        r4 = ", isInstant_=";
        r3 = r3.append(r4);	 Catch:{ all -> 0x0079 }
        r4 = r1.ʼ();	 Catch:{ all -> 0x0079 }
        r3 = r3.append(r4);	 Catch:{ all -> 0x0079 }
        r3 = r3.toString();	 Catch:{ all -> 0x0079 }
        r0.ˋ(r2, r3);	 Catch:{ all -> 0x0079 }
        monitor-exit(r1);	 Catch:{ all -> 0x0079 }
        goto L_0x0018;
    L_0x0079:
        r0 = move-exception;
        monitor-exit(r1);	 Catch:{ all -> 0x0079 }
        throw r0;
    L_0x007c:
        r0 = o.bnl.ˏ();	 Catch:{ all -> 0x0079 }
        r0 = r0.ॱˎ();	 Catch:{ all -> 0x0079 }
        r0 = r0.submit(r2);	 Catch:{ all -> 0x0079 }
        goto L_0x0037;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.huawei.appgallery.downloadengine.impl.DownloadService.doDownload(long):void");
    }

    @Nullable
    public IBinder onBind(Intent intent) {
        return this.ॱ;
    }

    public static boolean isAllDone() {
        for (WeakReference weakReference : ˊ) {
            Future future = (Future) weakReference.get();
            if (future != null && !future.isDone()) {
                return false;
            }
        }
        return true;
    }

    public static int getRunningTaskCount() {
        int i = 0;
        for (WeakReference weakReference : ˊ) {
            int i2;
            Future future = (Future) weakReference.get();
            if (future == null || future.isDone()) {
                i2 = i;
            } else {
                i2 = i + 1;
            }
            i = i2;
        }
        return i;
    }

    public static void ˊ() {
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            public void run() {
                Context ॱ = bnl.ˏ().ॱ();
                Intent intent = new Intent("ACTION_ONE_TASK_FINISHED");
                intent.setClass(ॱ, d.class);
                ll.ˏ(ॱ).ˋ(intent);
            }
        }, 15000);
    }
}
